package com.neusoft.chatserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.neusoft.chatserver.dto.UserQuery;
import com.neusoft.chatserver.dto.RespBean;
import com.neusoft.chatserver.entity.UserFriend;
import com.neusoft.chatserver.entity.Users;
import com.neusoft.chatserver.mapper.UserFriendMapper;
import com.neusoft.chatserver.mapper.UsersMapper;
import com.neusoft.chatserver.service.IUserFriendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuxp
 * @since 2021-07-14
 */
@Service
public class UserFriendServiceImpl extends ServiceImpl<UserFriendMapper, UserFriend> implements IUserFriendService {

    @Autowired
    UserFriendMapper userFriendMapper;

    @Autowired
    UsersMapper usersMapper;

    @Override
    public RespBean addFriend(UserFriend userFriend) {
        RespBean respBean = RespBean.ok(200, "Friend added successfully!");
        userFriendMapper.insert(userFriend);
        UserFriend userFriend1 = new UserFriend();
        userFriend1.setFriendId(userFriend.getUserId());
        userFriend1.setUserId(userFriend.getFriendId());
        userFriendMapper.insert(userFriend1);
        return respBean;
    }

    @Override
    public RespBean getAllFriend(Integer userId) {
        RespBean respBean = RespBean.ok(200, "Find friends successfully!");
        LambdaQueryWrapper<UserFriend> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserFriend::getUserId, userId);
        List<UserFriend> userFriends = userFriendMapper.selectList(wrapper);
        List<Integer> friends = new ArrayList<>();
        for(UserFriend userFriend : userFriends){
            friends.add(userFriend.getFriendId());
        }
        LambdaQueryWrapper<Users> userWrapper = Wrappers.lambdaQuery();
        userWrapper.in(Users::getUserId, friends);
        List<Users> users = usersMapper.selectList(userWrapper);
        List<UserQuery> list = new ArrayList<>();
        for (Users users1 : users){
            list.add(new UserQuery(users1.getUserId(),users1.getUserName(), users1.getUserRealname(), users1.getUserDeptId()));
        }
        respBean.setObj(list);
        return respBean;
    }

    @Override
    public RespBean deleteFriend(UserFriend userFriend) {
        RespBean respBean = RespBean.ok(200, "Friend deleted successfully!");
        LambdaQueryWrapper<UserFriend> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserFriend::getUserId, userFriend.getUserId()).eq(UserFriend::getFriendId, userFriend.getFriendId());
        userFriendMapper.delete(wrapper);
        LambdaQueryWrapper<UserFriend> wrapper1 = Wrappers.lambdaQuery();
        wrapper.eq(UserFriend::getUserId, userFriend.getFriendId()).eq(UserFriend::getFriendId, userFriend.getUserId());
        userFriendMapper.delete(wrapper1);
        return respBean;
    }


}
